package org.jeecg.modules.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


@Component
public class HashCode {
    // 应用id
    private static String appid;

    @Value("${school.appid}")
    public void setAppId(String appid) {
        HashCode.appid = appid;
    }

    // 应用id
    private static String appSecret;

    @Value("${school.appSecret}")
    public void setAppSecret(String appSecret) {
        HashCode.appSecret = appSecret;
    }

    // 应用id
    private static String url;

    @Value("${school.url}")
    public void setUrl(String url) {
        HashCode.url = url;
    }


    private static String gjurl;

    @Value("${school.gjurl}")
    public void setGjurl(String gjurl) {
        HashCode.gjurl = gjurl;
    }

    private static final String MAC_NAME = "HmacSHA1";
    private static final String ENCODING = "UTF-8";
    //二行制转字符串
    public static String byte2hex(byte[] b)
    {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toUpperCase();
    }
    public static String HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
        byte[] data = encryptKey.getBytes(ENCODING);
        // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
        // 生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = Mac.getInstance(MAC_NAME);
        // 用给定密钥初始化 Mac 对象
        mac.init(secretKey);
        byte[] text = encryptText.getBytes(ENCODING);
        // 完成 Mac 操作
        return byte2hex(mac.doFinal(text)).toLowerCase();
    }



    public static String resaultUrl(String phone){
        System.out.println(appid);
        System.out.println(appSecret);
        String timestamp= String.valueOf(System.currentTimeMillis());
        System.out.println(timestamp);
        //字符串和秘钥
        String str1=appid+timestamp+phone;
        String res="";
        try {
             res=HmacSHA1Encrypt(str1,appSecret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url+"?phone="+phone+"&appid="+appid+"&encryptvalue="+res+"&timestamp="+timestamp;
    }

    public static String resaultGjUrl(){
        return gjurl;
    }



	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		//String str1="be5a017c-6f84-f0e5-1b32-a437ba5f989e"+"1542616969505";
//		String AppId="be5a017c6f84f0e51b32a437ba5f989e";
//		String timestamp= String.valueOf(System.currentTimeMillis());
//
//        System.out.println(timestamp);
//		String str1=AppId+timestamp;
//		String AppSecret="938029664987617b";
//
//
//		String res=HmacSHA1Encrypt(str1,AppSecret);
//        System.out.println(res);
		// TODO Auto-generated method stub
        String s = resaultUrl("19961862317");
        System.out.println("s--------"+s);

    }

}
